﻿using System;
using System.Collections.Generic;

using Caliburn.Micro;

using FakeItEasy.Configuration;

namespace Cooking.Client.Specs.Support
{
    public static class Extensions
    {
        public static CoroutineEnumerator GetCoroutineEnumerator(this IEnumerable<IResult> coroutines)
        {
            return new CoroutineEnumerator(coroutines);
        }

        public static void Invokes<T>(this IVoidConfiguration voidConfiguration, int argumentIndex, T data) where T : class
        {
            voidConfiguration.Invokes(action => 
            {
                if (argumentIndex == Argument.Last)
                {
                    argumentIndex = action.Arguments.Count - 1;
                }

                var callback = action.Arguments[argumentIndex] as Action<T>;
                if (callback == null)
                {
                    throw new InvalidOperationException("The specified argument is not of type Action<" + typeof (T).Name + ">");
                }

                callback(data);
            });
        }
    }

    public static class Argument
    {
        public const int Last = int.MaxValue;
    }
}